﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Deployment.Data.Deploy.Steps.Tasks
{
    public abstract class AbstractTaskControl : UserControl
    {

        public static readonly DependencyProperty EnvironmentsFileProperty =
            DependencyProperty.Register("EnvironmentsFile",
                typeof(Environment.EnvironmentsFile),
                typeof(AbstractTaskControl), null);

        //public static readonly DependencyProperty CurrentStepProperty =
        //    DependencyProperty.Register("CurrentStep",
        //        typeof(Deploy.Steps.Step),
        //        typeof(AbstractTaskControl), null);

        //public static readonly RoutedEvent EnvironmentsFileChangedEvent =
        //    EventManager.RegisterRoutedEvent("EnvironmentsFileChanged",
        //        RoutingStrategy.Bubble,
        //        typeof(RoutedEventHandler),
        //        typeof(AbstractTaskControl));

        //public static readonly RoutedEvent CurrentStepChangedEvent =
        //    EventManager.RegisterRoutedEvent("CurrentStepChanged",
        //        RoutingStrategy.Bubble,
        //        typeof(RoutedEventHandler),
        //        typeof(AbstractTaskControl));

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        protected void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs(property, GetValue(property), value));
        }

        public Environment.EnvironmentsFile EnvironmentsFile
        {
            get { return (Environment.EnvironmentsFile)GetValue(EnvironmentsFileProperty); }
            set 
            { 
                SetValueDp(EnvironmentsFileProperty, value);
                process_EvnvironmentsFileChanged();
            }
        }

        //public Deploy.Steps.Step CurrentStep
        //{
        //    get { return (Deploy.Steps.Step)GetValue(CurrentStepProperty); }
        //    set { SetValueDp(CurrentStepProperty, value); }
        //}

        //public event RoutedEventHandler EnvironmentsFileChanged
        //{
        //    add { AddHandler(EnvironmentsFileChangedEvent, value); }
        //    remove { RemoveHandler(EnvironmentsFileChangedEvent, value); }
        //}

        //public event RoutedEventHandler CurrentStepChanged
        //{
        //    add { AddHandler(CurrentStepChangedEvent, value); }
        //    remove { RemoveHandler(CurrentStepChangedEvent, value); }
        //}


        public AbstractTaskControl()
            : base()
        {
            this.PropertyChanged += AbstractTaskControl_PropertyChanged;
            //this.CurrentStepChanged += process_CurrentStepChanged;
            //this.EnvironmentsFileChanged += process_EvnvironmentsFileChanged;
        }

        protected abstract void process_EvnvironmentsFileChanged();

        //protected abstract void process_CurrentStepChanged(object sender, RoutedEventArgs e);


        void AbstractTaskControl_PropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //Handle the ConnectionsFileProperty and DeploymentFileProperty changing into their own events.
            //if (e.Property.Equals(EnvironmentsFileProperty))
            //    RaiseEnvironmentChangedEvent();
            ////else if (e.Property.Equals(CurrentStep))
            ////    RaiseDeploymentChangedEvent();
        }

        //void RaiseEnvironmentChangedEvent()
        //{
        //    RoutedEventArgs newEventArgs = new RoutedEventArgs(AbstractTaskControl.EnvironmentsFileChangedEvent);
        //    RaiseEvent(newEventArgs);
        //}

        //void RaiseDeploymentChangedEvent()
        //{
        //    RoutedEventArgs newEventArgs = new RoutedEventArgs(AbstractTaskControl.CurrentStepChangedEvent);
        //    RaiseEvent(newEventArgs);
        //}

    }
}
